package com.extension.company.model.main;


import com.extension.company.Constant;
import com.extension.company.model.entity.LeftListBean;
import com.extension.company.model.entity.RightListBean;
import com.extension.company.common.http.RetrofitHelper;
import com.extension.company.contract.main.MakeMoneyFragmentContract;
import com.extension.company.model.api.RetrofitUrlApi;
import com.extension.company.presenter.home.MakeMoneyPrester;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者:zwk
 * 联系方式：497981239@qq.com
 * 时间：on 2019/7/4 11:20
 * 功能： 双list的网路请求
 */
public class MakeMoneyFragmentMode implements MakeMoneyFragmentContract.Mode {

	private RetrofitUrlApi retrofitUrlApi = RetrofitHelper.getRetrofit().create(RetrofitUrlApi.class);

	/**
	 * 请求左侧list的数据
	 *
	 * @param callBack 返回给p层数据
	 */
	public void leftData(final MakeMoneyPrester.LeftCallBack callBack) {
		retrofitUrlApi.left(Constant.SIGN)
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<LeftListBean>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(LeftListBean leftListBean) {
						if (leftListBean.getCode() == 200) {
							callBack.callBack(leftListBean.getData().getCategory());
						}


					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});
	}

	/**
	 * 右侧的网络请求
	 *
	 * @param leftId        分类的id
	 * @param count         页码数
	 * @param rightCallBack 详细分类的回调
	 */
	public void rightData(final int leftId, int count, final MakeMoneyPrester.RightCallBack rightCallBack) {
		retrofitUrlApi.right(Constant.SIGN, leftId, count)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<RightListBean>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(RightListBean rightListBean) {
						if (200 == rightListBean.getCode()) {
							if (null != rightListBean.getData().getGoods()) {
								rightCallBack.callBack(rightListBean.getData().getGoods(), leftId);
							}

						}

					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});

	}

}
